﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.U2D;
using UnityEngine.UI;


namespace SuperScrollView
{

    public class BannerList : MonoBehaviour
    {
        BannerItem.OnSelectDelegate OnSelctItem = null;
        BannerItem.OnShowDelegate OnShowItem = null;
        public LoopListView2 mLoopListView;


        public bool m_IsOpenNavIcon;
        public Transform m_ObjNavigation;

        public SpriteAtlas navAtlas;

        public string m_CurrentNavIcon;
        public string m_NormalNavIcon;

        private List<Image> m_NavigationList = new List<Image>();
        //-> 开启 导航图标模式时 使用以下变量
        public float m_IconSize = 20;
        public float m_IconSpacing = 5.0f;
        public int m_CurrentIndex = 0;

        private bool isDrag = false;

        int mItemCount = 1;

        int mCurSelectedValue = 0;

        float mWaitTime = 3.0f;

        public bool m_IsCanAutoMove = false;

        public int CurSelectedValue
        {
            get { return mCurSelectedValue; }
        }

        public void Init(int count, BannerItem.OnSelectDelegate selctItemFunc, BannerItem.OnShowDelegate showItemFunc)
        {
            mItemCount = count;
            OnSelctItem = selctItemFunc;
            OnShowItem = showItemFunc;

            m_NavigationList.Clear();
            InitNavigation();
        }
        public int GetShowIndex()
        {
            return m_CurrentIndex;
        }

        public void RefreshList()
        {
            mLoopListView.RefreshAllShownItem();
        }

        public void MovePanelToItemIndex(int targetIndex, float offset = 0)
        {
            mLoopListView.MovePanelToItemIndex(targetIndex, offset);
        }

        // Use this for initialization
        void Start()
        {
            //set all snap callback.
            mLoopListView.mOnSnapNearestChanged = OnSnapTargetChanged;
            mLoopListView.InitListView(-1, OnGetItemByIndex);
            mLoopListView.mOnBeginDragAction = OnBeginDrag;
            mLoopListView.mOnEndDragAction = OnEndDrag;
            mLoopListView.mOnSnapItemFinished = OnSnapTargetFinished;
            StartAutoScroll();
        }

        public LoopListViewItem2 OnGetItemByIndex(LoopListView2 listView, int index)
        {
            LoopListViewItem2 item = listView.NewListViewItem("ShowItem");
            BannerItem itemScript = item.GetComponent<BannerItem>();
            if (item.IsInitHandlerCalled == false)
            {
                item.IsInitHandlerCalled = true;
                itemScript.Init();
                itemScript.OnSelectItem = OnSelctItem;
                itemScript.OnShowItem = OnShowItem;
            }
            int firstItemVal = 1;
            int itemCount = mItemCount;
            int val;
            if (index >= 0)
            {
                val = index % itemCount;
            }
            else
            {
                val = itemCount + ((index + 1) % itemCount) - 1;
            }
            val += firstItemVal;
            itemScript.SetItemData(val);
            return item;
        }

        void OnSnapTargetChanged(LoopListView2 listView, LoopListViewItem2 item)
        {
            int index = listView.GetIndexInShownItemList(item);
            if (index < 0)
            {
                return;
            }
            BannerItem itemScript = item.GetComponent<BannerItem>();
            mCurSelectedValue = itemScript.Value;
            m_CurrentIndex = mCurSelectedValue - 1;
            RefreshNavigation(m_CurrentIndex);

            OnListViewSnapTargetChanged(listView, index);
        }


        void OnSnapTargetFinished(LoopListView2 listView, LoopListViewItem2 item)
        {

        }

        void OnListViewSnapTargetChanged(LoopListView2 listView, int targetIndex)
        {
            int count = listView.ShownItemCount;
            for (int i = 0; i < count; ++i)
            {
                LoopListViewItem2 item2 = listView.GetShownItemByIndex(i);
                BannerItem itemScript = item2.GetComponent<BannerItem>();
                itemScript.SetShow(i == targetIndex);
            }
        }

        void OnBeginDrag()
        {
            StopAutoScroll();
            isDrag = true;
        }

        void OnEndDrag()
        {
            isDrag = false;
            StartAutoScroll();
        }

        public void StartAutoScroll()
        {
            if (isDrag)
                return;
            if (m_IsCanAutoMove)
                StartCoroutine(AutoScroll());
        }

        public void StopAutoScroll()
        {
            StopAllCoroutines();
        }

        IEnumerator AutoScroll()
        {
            yield return new WaitForSeconds(mWaitTime);
            DoAutoScrollPage();
            yield return new WaitForSeconds(1.0f);
            StartAutoScroll();
        }

        void DoAutoScrollPage()
        {
            int curNearestItemIndex = mLoopListView.CurSnapNearestItemIndex;
            int index = curNearestItemIndex + 1;
            mLoopListView.SetSnapTargetItemIndex(index, 0.2f);
        }

        void RefreshNavigation(int showIndex)
        {
            if (m_IsOpenNavIcon)
            {
                int length = m_NavigationList.Count;
                for (int i = 0; i < length; i++)
                {
                    Image img = m_NavigationList[i];
                    img.sprite = showIndex == i ? navAtlas.GetSprite(m_CurrentNavIcon) : navAtlas.GetSprite(m_NormalNavIcon);
                    img.SetNativeSize();
                }
            }
        }

        void InitNavigation()
        {
            if (m_IsOpenNavIcon && mItemCount > 1)
            {
                if (m_ObjNavigation == null)
                {
                    m_ObjNavigation = transform.Find("m_ObjNavigation");
                }
                float[] posArray = new float[mItemCount];
                if (mItemCount == 1)
                {
                    posArray[0] = 0;
                }
                else
                {
                    float startX = -mItemCount / 2 * m_IconSpacing;
                    for (int i = 0; i < mItemCount; i++)
                    {
                        posArray[i] = startX + m_IconSpacing * i;
                    }
                }

                for (int i = 0; i < mItemCount; i++)
                {
                    GameObject icon = null;
                    if (m_ObjNavigation.Find(string.Format("icon{0}", i)) != null)
                    {
                        icon = m_ObjNavigation.Find(string.Format("icon{0}", i)).gameObject;
                    }
                    else
                    {
                        icon = new GameObject(string.Format("icon{0}", i));
                    }

                    icon.transform.parent = m_ObjNavigation;
                    Image img = null;
                    if (icon.GetComponent<Image>() == null)
                    {
                        img = icon.AddComponent<Image>();
                    }
                    else
                    {
                        img = icon.GetComponent<Image>();
                    }
                    img.sprite = i == m_CurrentIndex ? navAtlas.GetSprite(m_CurrentNavIcon) : navAtlas.GetSprite(m_NormalNavIcon);
                    img.rectTransform.sizeDelta = new Vector2(m_IconSize, m_IconSize);
                    img.SetNativeSize();
                    icon.transform.localScale = Vector3.one;
                    icon.transform.localPosition = new Vector3(posArray[i], 0, 0);
                    m_NavigationList.Add(img);
                }
            }
        }
    }
}

